// 链表来实现栈
// es5 没有class 关键字
class ListNode {
    constructor(val){
        this.val = val;
        this.next = null;
    }
}
class LinkedListStack {
    //私有的,只能在内部使用，封装实现的细节，保护类不能被随意修改
    //栈顶指针
    #stackPeek;
    // 栈的大小
    #size = 0;
    constructor(){
        this.#stackPeek = null;
    }
    push(num){
        const node = new ListNode(num);
        node.next = this.#stackPeek;
        this.#stackPeek = node;
        this.#size++;
    }
    pop(){
        if(this.#size === 0) return null;
        const val = this.#stackPeek.val;
        this.#stackPeek = this.#stackPeek.next;
        this.#size--;
        return val;
    }
    peek(){
        return this.#size === 0 ? null : this.#stackPeek.val;
    }
    // 访问属性
    get size(){
        return this.#size;
    }
    isEmpty(){
        return this.size === 0;
    }
    toArray(){
        let node = this.#stackPeek;
        const res = [];
        while(node){
            res.push(node.val);
            node = node.next;
        }
        return res;
    }
}
const LinkedListStack = new LinkedListStack();
console.log(LinkedListStack.#stackPeek);
